import settings
import threading
from util.module_util import get_modules, get_first_func_by_ann
import importlib
import log_utils
import app
from db.db_utils import init_db


if __name__ == "__main__":

    # 初始化单例容器
    app = app.App()

    # 初始化日志配置
    log = log_utils.init_log()
    app.log = log

    # init db
    init_db()

    run_list = []
    api_path = settings.api_path
    api_attr = settings.api_attr
    modules = get_modules(api_path)

    # 将包下的所有模块,逐个导入,查找run方法并通过反射获取实例
    for module in modules:
        module_cls = importlib.import_module(module, api_path)
        module_path = api_path + "/" + module[1:]
        func_name = get_first_func_by_ann(module_path, api_attr)

        if func_name is None:
            continue
        func = getattr(module_cls, func_name)
        run_list.append(func)
    # 开启爬虫线程
    for run in run_list:
        threading.Thread(target=run).start()
